﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Xml.Serialization;

namespace MShop.Core.Util
{
    public static class SerializeUtil
    {
        public static string Serialize(object obj)
        {
            using (System.IO.StringWriter sw = new System.IO.StringWriter())
            {
                XmlSerializer serializer = new XmlSerializer(obj.GetType());
                serializer.Serialize(sw, obj);
                return sw.ToString();
            }
        }
        public static T Deserialize<T>(string content)
        {
            
            try
            {
                XmlSerializer serializer = new XmlSerializer(typeof(T));
                using (System.IO.StringReader sr = new System.IO.StringReader(content))
                {
                    return (T)serializer.Deserialize(sr);
                }
            }
            catch
            {
                T t = default(T);
                return t;
            }


        }

        public static object Deserialize(Type type,string content)
        { 
                XmlSerializer serializer = new XmlSerializer(type);
                using (System.IO.StringReader sr = new System.IO.StringReader(content))
                {
                    return serializer.Deserialize(sr);
                } 
        }
    }
}